module one {
	function rand() {
		return 16777215 * Math.random()
	}
	/**
	 * 随机打乱
	 */
	export function shuffleArray(a: any[], range: number = 0) {
		if (0 >= range || range > a.length) range = a.length;
		for (var c = range - 1; 0 <= c; c--) {
			var d = 0 | rand() % (c + 1),
				e = a[c];
			a[c] = a[d];
			a[d] = e
		}
	}

    /**
     * 前闭后闭
     * */
	export function randomInt(min: number, max: number) {
		if (min > max) {
			let t = max;
			max = min;
			min = t;
		}
		return Math.floor(Math.random() * (max - min + 1)) + min;
	}

	/**
	 * 随机 1，-1
	 */
	export function randomDic(): 1 | -1 {
		return randomInt(0, 1) == 0 ? -1 : 1;
	}

	export function clone(frame) {
		var result;
		if (frame instanceof Array) {
			result = [];
		}
		else if (frame instanceof Object) {
			result = {};
		}
		else {
			return frame;
		}

		for (var key in frame) {
			if (frame[key] instanceof Array) {
				result[key] = clone(frame[key]);
			}
			else if (frame[key] instanceof Object) {//
				result[key] = clone(frame[key]);
			}
			else {
				result[key] = frame[key];
			}
		}
		return result;
	}

	/**
	 * 按照字符数量截取
	 */
	export function cutName(name: string, count: number = 7): string {
		if (name.length <= count) {
			return name;
		}

		return name.substr(0, count) + "...";
	}

	function measureTextWidth(text: string, style: egret.TextField): number {
		let italic: boolean = style.italic;
		let bold: boolean = style.bold;
		let size: number = style.size;
		let fontFamily: string = style.fontFamily || egret.TextField.default_fontFamily;
		return egret.sys.measureText(text, fontFamily, size, bold, italic);
	}

	/**
	 * 按照文本长度截字
	 */
	export function cutText(text: egret.TextField, msg: string, width: number = 300): string {
		if (measureTextWidth(msg, text) <= width) {
			return msg;
		}
		let max = msg.length;
		let min = 0;
		while (true) {
			if (min >= max - 1) {
				return msg.substr(0, min) + "...";
			}
			if (measureTextWidth(msg.substr(0, Math.floor((max + min) / 2)), text) <= width - text.size * 2) {
				min = Math.floor((max + min) / 2);
			}
			else {
				max = Math.floor((max + min) / 2);
			}
		}
	}

	/**
	 * 增加 万、亿 显示
	 * @cash 数量
	 * @fixed 保留小数点后的尾数
	 */
	export function formatCash(cash: number, fixed: number = 2): string {
		if (cash < 10000) {
			return cash + "";
		}

		cash = cash / 10000;
		if (cash < 10000) {
			return cash.toFixed(fixed) + "万";
		}

		cash = cash / 10000;
		if (cash < 10000) {
			return cash.toFixed(fixed) + "亿";
		}
	}

	/**
	 * 每3位加一逗号
	 */
	export function formatCount(count: number): string {
		let cnt: string = count + "";
		let result: string = "";
		for (let i = 0; i < cnt.length; i++) {
			if (i % 3 == 0 && i != 0) {
				result = "," + result;
			}
			result = cnt.charAt(cnt.length - 1 - i) + result;
		}
		return result;
	}

	/**
	 * 每3位加一逗号
	 * param count 数量
	 * param fixed 留几位小数
	 */
	export function formatCount2(count: number, fixed: number = 2): string {
		count = Math.floor(count);
		if (count < 1000) {
			return count + "";
		}
		let length = 0;
		let tempC = count;
		while (tempC >= 10) {
			tempC /= 10;
			length++;
		}

		let dd = length % 3;
		length -= dd;

		for (let i = 0; i < length; i++) {
			count /= 10;
		}

		count = Math.floor(count * Math.pow(10, fixed)) / Math.pow(10, fixed);

		let d = ["", "k", "m", "b", "t", "a", "b", "c", "d", "e", "f", "g", "h"];
		return count + d[length / 3];
	}

	let isPhoneX = -1;
	export function getFixRap(): number {
		if (isPhoneX == -1) {
			if (egret.Capabilities.runtimeType == egret.RuntimeType.WXGAME) {
				let info = wx.getSystemInfoSync()
				var model = info.model
				if (model.search('iPhone X') != -1) {
					isPhoneX = 1;
				}
			}
			else {
				isPhoneX = 0;
			}
		}
		let rap = isPhoneX == 1 ? 40 : 0;
		return rap;
	}

	export function fixLeft(item: eui.Component | eui.Group | eui.Image, left: number): void {
		item.left = left + getFixRap();
	}

	export function fixWidth(item: eui.Component | eui.Group, width: number): void {
		let rap = getFixRap();

		if (!isNaN(width)) {
			item.width = width - rap;
		}
	}

}
